/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.activity;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import org.internna.iwebjtracker.activity.model.RecordedActivity;
import org.internna.iwebjtracker.project.model.IWebJTrackerUser;

/**
 * Store for activities.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public abstract class AbstractActivityHolder<T extends RecordedActivity, U extends IWebJTrackerUser> implements ActivityHolder<T, U> {

    private List<T> activities;

    /**
     * Stores a new element on the list creating one first if needed.
     *
     * @param activity any
     */
    @Override public final void store(T activity) {
        if (activity != null) {
            if (activities == null) {
                activities = new ArrayList<T>(1);
            }
            activities.add(activity);
        }
    }

    /**
     * The activities stored during this request execution.
     *
     * @return a non null unmodifiable ordered collection of activities that have hapenned on this project.
     */
    @Override public final List<T> getPerformedActivities() {
        return activities == null ? Collections.EMPTY_LIST : activities;
    }

    /**
     * Empties the list of activities.
     */
    @Override public final void clear() {
        if (activities != null) {
            activities.clear();
        }
        activities = null;
    }

}
